﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spider.Entitys.Spider;
using SpiderCore.DB;
using Spider.Entitys.MongoDB;
using SpiderCore.Loggers;

namespace SpiderCore.Pipeline
{
    public class PipelineHandlerMongoDB : IPipelineHandler
    {
        public static int DbCount = 0;
        /// <summary>
        /// 初始化日志类型
        /// </summary>
        private readonly ISpiderLogger log = new SpiderMongoDBLog(typeof(PipelineHandlerMongoDB));
        IDB db = new MongoDb();

        /// <summary>
        /// 过滤规则
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool Filter(MessageContext message)
        {
            return true;
        }

        /// <summary>
        /// 处理 把页面持久化到 MongoDB 中
        /// </summary>
        /// <param name="message"></param>
        public void Extract(MessageContext message)
        {
            var res = message as HtmlMessageContext;
            db.Set<listHTML>("", new listHTML()
            {
                Html = res.Text,
                FenLei = "",
                Source = res.Response.Request.Source,
                Url = res.Response.Request.Url,
                CreateTime = DateTime.Now,
                Request = res.Response.Request
            });
            DbCount++;
            Console.WriteLine("已入库：" + DbCount);
        }


    }
}
